
package com.chief.test.controller;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidMonitor {



    @Bean
    public ServletRegistrationBean initServlet(){
        ServletRegistrationBean<StatViewServlet> registrationBean =
                new ServletRegistrationBean<>(new StatViewServlet(),
                        "/druid/*");

        registrationBean.addInitParameter("jmxUrl","service:jmx:rmi:///jndi/rmi://127.0.0.1:9876/jmxrmi");
        registrationBean.addInitParameter("loginUsername","admin");
        registrationBean.addInitParameter("loginPassword","admin");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean initFilter(){
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/druid/*");

        Map<String,String> initParam = new HashMap<String, String>();
        //排除拦截哪些请求
        initParam.put("exclusions","*.js,*.css,/druid/*");
        filterRegistrationBean.setInitParameters(initParam);
        return filterRegistrationBean;
    }

}
